package com.zdpx.pxframework.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdpx.pxframework.core.validator.group.AddGroup;
import com.zdpx.pxframework.core.validator.group.UpdateGroup;
import com.zdpx.pxframework.core.vo.PageResult;
import com.zdpx.pxframework.core.vo.Result;
import com.zdpx.pxframework.system.api.RoleMenuControllerApi;
import com.zdpx.pxframework.system.entity.RoleMenu;
import com.zdpx.pxframework.system.service.RoleMenuService;
import com.zdpx.pxframework.system.vo.query.RoleMenuQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 角色菜单关联 接口控制器
 *
 * @Description:
 * @Author: Cheng XiaoXiao
 * @Date: 2022-02-27 15:20:14
 */
@RestController
@RequestMapping("/role-menu")
public class RoleMenuController implements RoleMenuControllerApi {
    @Resource
    private RoleMenuService roleMenuService;


    /**
     * 获取条件查询信息
     *
     * @param roleMenuQuery 查询条件
     * @return 查询封装
     */
    private LambdaQueryWrapper<RoleMenu> getRoleMenuQueryWrapper(RoleMenuQuery roleMenuQuery) {
        LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(roleMenuQuery.getRoleId())) {
            wrapper.eq(RoleMenu::getRoleId, roleMenuQuery.getRoleId());
        }
        if (!StringUtils.isEmpty(roleMenuQuery.getMenuId())) {
            wrapper.eq(RoleMenu::getMenuId, roleMenuQuery.getMenuId());
        }
        return wrapper;
    }

    /**
     * 按照条件查询角色菜单关联信息
     *
     * @param roleMenuQuery 查询条件
     * @return 条件结果信息
     */
    @Override
    @GetMapping
    public Result<List<RoleMenu>> list(RoleMenuQuery roleMenuQuery) {
        return Result.success(roleMenuService.list(getRoleMenuQueryWrapper(roleMenuQuery)));
    }

    /**
     * 分页及条件查询角色菜单关联信息
     *
     * @param page          页码
     * @param size          分页大小
     * @param roleMenuQuery 查询条件
     * @return 分页结果信息
     */
    @Override
    @GetMapping("/{size}/{page}")
    public Result<PageResult<RoleMenu>> search(@PathVariable("size") Long size, @PathVariable("page") Long page, RoleMenuQuery roleMenuQuery) {
        if (page <= 0) {
            page = 1L;
        }
        if (size <= 0) {
            size = 10L;
        }
        Page<RoleMenu> pageParam = new Page<>(page, size);

        roleMenuService.page(pageParam, getRoleMenuQueryWrapper(roleMenuQuery));
        PageResult<RoleMenu> pageResult = new PageResult<RoleMenu>(pageParam.getTotal(), pageParam.getSize(), pageParam.getCurrent(), pageParam.getRecords());

        return Result.success(pageResult);
    }

    /**
     * 根据ID获取角色菜单关联详情
     *
     * @param id 角色菜单关联ID
     * @return 角色菜单关联详细信息
     */
    @Override
    @GetMapping("/{id}")
    public Result<RoleMenu> detail(@PathVariable("id") String id) {
        return Result.success(roleMenuService.detailById(id));
    }

    /**
     * 增加角色菜单关联信息
     *
     * @param roleMenu 角色菜单关联增加实体
     * @return 增加后的角色菜单关联信息
     */
    @Override
    @PostMapping
    public Result<RoleMenu> add(@RequestBody @Validated(AddGroup.class) RoleMenu roleMenu) {
        roleMenuService.saveRoleMenu(roleMenu);
        return Result.success(roleMenu);
    }

    /**
     * 修改角色菜单关联信息
     *
     * @param id       角色菜单关联ID
     * @param roleMenu 角色菜单关联修改实体
     * @return 修改后的角色菜单关联信息
     */
    @Override
    @PutMapping("/{id}")
    public Result<RoleMenu> updateById(@PathVariable("id") String id, @RequestBody @Validated(UpdateGroup.class) RoleMenu roleMenu) {
        roleMenu.setId(id);
        roleMenuService.updateRoleMenuById(roleMenu);
        return Result.success(roleMenuService.getById(id));
    }


    /**
     * 删除角色菜单关联信息
     *
     * @param id 角色菜单关联ID
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") String id) {
        roleMenuService.deleteById(id);
        return Result.success(true);
    }

    /**
     * 删除角色菜单关联信息
     *
     * @param idList 角色菜单关联ID数组
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/batch-remove")
    public Result<Boolean> batchRemove(@RequestBody List<String> idList) {
        roleMenuService.removeByIds(idList);
        return Result.success(true);
    }
}
